import Tpl from './index.ejs';
import PinTpl from './pin.ejs';
import PinAddTpl from './pinAdd.ejs';
import ZhuanTpl from './zhuan.ejs';
import ZhuanAddTpl from './zhuanAdd.ejs';

import ListTpl from './list.ejs';
import PinAreaAddTpl from './pinAreaAdd.ejs';
import ZhuanAreaAddTpl from './zhuanAreaAdd.ejs';
import PinCarAddTpl from './pinCarAdd.ejs';
import ZhuanCarAddTpl from './zhuanCarAdd.ejs';
import AddStationTpl from './addStation.ejs';
import AddAirportTpl from './addAirport.ejs';

import Tools from '../tools/tools.js';

class Product {

	init ($wrap, router) {
		let self = this;

		this.render($wrap, router, function () {

			$('.ui.checkbox').checkbox();
	        $('.dropdown').dropdown();

			self.$listWrap = $('#listWrap');
			self.reqData = {};
			self.listData = [];

			self.bindEvent();
		});
	}

	bindEvent () {
		let self = this;

		$('#pinCheckBtn').on('click', function () {
			self.pinCheckForm();
		});

		$('#pinAddBtn').on('click', function () {
			self.pinAddStationFrom();
		});

		$('#zhuanCheckBtn').on('click', function () {
			self.zhuanCheckForm();
		});

		$('#zhuanAddBtn').on('click', function () {
			self.zhuanAddStationFrom();
		});
	}

	pinCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/queryProductList',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.listData = data;
					self.$listWrap.html(ListTpl(data));
				}
			}
        });
	}

	pinAddStationFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/terminal/info/queryByCity',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					if (Math.floor(self.reqData.serviceType) <= 2) {
						$('.pin-add-station').html(AddAirportTpl(data));
					}
					else {
						$('.pin-add-station').html(AddStationTpl(data));
					}

					$('.dropdown').dropdown();

					$('#pinAddBtn').off('click');
					$('#pinAddBtn').on('click', function () {
						self.pinAddAreaFrom();
					});
				}
			}
        });
	}

	pinAddAreaFrom () {
		let self = this;

		if (!$('input[name=station]').val()) {
			alert('请先选择');
			return;
		}

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/qeuryDepAndArr',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-add-area').html(PinAreaAddTpl(data));
					$('.ui.checkbox').checkbox();

					$('#pinAddBtn').off('click');
					$('#pinAddBtn').on('click', function () {
						self.pinAddCarFrom();
					});
				}
			}
        });
	}

	pinAddCarFrom () {
		let self = this;

		if (!$('input[name=depPlace]').val()) {
			alert('请先选择');
			return;
		}

		if (!$('input[name=arrPlace]').val()) {
			alert('请先选择');
			return;
		}

        $.ajax({
			url: '/car/models/queryCarModel',
			method: 'post',
			data: JSON.stringify({}),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-add-car').html(PinCarAddTpl(data));
					$('.ui.checkbox').checkbox();

					$('#pinAddBtn').text('添加');
					$('#pinAddBtn').off('click');
					$('#pinAddBtn').on('click', function () {
						self.pinAddFrom();
					});
				}
			}
        });
	}

	pinAddFrom () {
		let self = this;

		if (!Tools.getCarFromData().length) {
			alert('请先选择');
			return;
		}

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1,
			carProductModels: Tools.getCarFromData()
        }, self.reqData);

        $.ajax({
			url: '/product/info/addProduct',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	zhuanCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/queryProductList',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.listData = data;
					self.$listWrap.html(ListTpl(data));
				}
			}
        });
	}

	zhuanAddStationFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2
        }, Tools.getFromData());

        $.ajax({
			url: '/terminal/info/queryByCity',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-add-station').html(AddAirportTpl(data));
					$('.dropdown').dropdown();

					$('#zhuanAddBtn').off('click');
					$('#zhuanAddBtn').on('click', function () {
						self.zhuanAddAreaFrom();
					});
				}
			}
        });
	}

	zhuanAddAreaFrom () {
		let self = this;

		if (!$('input[name=station]').val()) {
			alert('请先选择');
			return;
		}

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/qeuryDepAndArr',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-add-area').html(ZhuanAreaAddTpl(data));
					$('.ui.checkbox').checkbox();

					$('#zhuanAddBtn').off('click');
					$('#zhuanAddBtn').on('click', function () {
						self.pinAddCarFrom();
					});
				}
			}
        });
	}

	zhuanAddCarFrom () {
		let self = this;

		if (!$('input[name=depPlace]').val()) {
			alert('请先选择');
			return;
		}

		if (!$('input[name=arrPlace]').val()) {
			alert('请先选择');
			return;
		}

        $.ajax({
			url: '/car/models/queryCarModel',
			method: 'post',
			data: JSON.stringify({}),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.pin-add-car').html(ZhuanCarAddTpl(data));
					$('.ui.checkbox').checkbox();

					$('#zhuanAddBtn').text('添加');
					$('#zhuanAddBtn').off('click');
					$('#zhuanAddBtn').on('click', function () {
						self.pinAddFrom();
					});
				}
			}
        });
	}

	zhuanAddFrom () {
		let self = this;

		if (!Tools.getCarFromData().length) {
			alert('请先选择');
			return;
		}

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2,
			carProductModels: Tools.getCarFromData()
        }, self.reqData);

        $.ajax({
			url: '/product/info/addProduct',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	render ($wrap, router, callback) {
		if (!router) {
            $wrap.html(Tpl());
        }
        else {
            switch (router) {
               	case 'pin':

					Tools.getCity(function (data) {
						$wrap.html(PinTpl(data));
						callback();
					});
               		break;
                case 'pinAdd':

					Tools.getCity(function (data) {
						$wrap.html(PinAddTpl(data));
						callback();
					});
               		break;
                case 'zhuan':
               		$wrap.html(ZhuanTpl());
					callback();
               		break;
                case 'zhuanAdd':

					Tools.getCity(function (data) {
						$wrap.html(ZhuanAddTpl(data));
						callback();
					});
               		break;
               	default:
               		break;
            }
        }
	}
}

export default new Product();
